// 引入连接
const connection = require('../../data/index');
// 登陆页面
module.exports.login = (req, res) => {
    res.render('login')
}
// 注册页面
module.exports.zc = (req, res) => {
    res.render('zc')
}
// 添加页面
module.exports.addUser = (req, res) => {
    var str = [req.body.users_email, req.body.users_name, req.body.users_password]
    console.log(str)
    connection.query('insert into users (users_email,users_name,users_password) values(?,?,?)', str, (error, result) => {
        if (error) {
            console.log(error)
        } else {
            if (result) {
                res.json({
                    code: '1000'
                })
            }
        }
    })
}
// 登陆页面
module.exports.loginUser = (req, res) => {
    var userdata = [req.query.userName, req.query.userName, req.query.pass]
    connection.query('select * from users where (users_name =? or users_email=?) and users_password = ?', userdata, (error, result) => {
        if (error) {
            console.log(error)
        } else {
            if (result.length != 0) {
                // 储存用户数据
                req.session.user = result;
                let userdata = req.session.user;
                req.session.login = true;
                res.json({
                    code: "1001",
                    msg: '登陆成功'
                })
            } else {
                res.json({
                    code: "1002",
                    msg: "用户或密码输入错误",
                })
            }
        }
    })
}